﻿package anshe.object
{
	import org.dstart.csuper.SuperSprite;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.events.Event;

	public class SliderObject extends SuperSprite
	{
		public static const CHANGE_PLACE:String	= "changePlace";	/** 对象位置发生改变 **/
		public static const CHANGE_DONE:String	= "changeDone";		/** 对象调节完毕后触发 **/

		public var sprLight:Sprite;			/** 高亮显示对象 **/
		public var sprButton:Sprite;		/** 滑动条控制按钮 **/
		public var txtPercent:TextField;	/** 显示出来的文字 **/
		
		private var m_minNumber:Number;		/** 最小显示数值 **/
		private var m_maxNumber:Number;		/** 最大显示数值 **/
		
		public function SliderObject()
		{
			InitObject();
			InitListener();
		}
		
		/** 读取当前位置的值 **/
		public function get percent():Number
		{
			var p:Number = this.sprButton.x / (this.sprLight.width - this.sprButton.width);
			
			return Math.ceil(p * (m_maxNumber - m_minNumber) + m_minNumber);
		}
		
		/** 设置当前的位置 **/
		public function set percent(num:Number):void
		{
			num = Boolean(num < m_minNumber) ? m_minNumber : Boolean(num > m_maxNumber) ? m_maxNumber : num;

			var p:Number = (num - m_minNumber) / (m_maxNumber - m_minNumber);
			
			this.sprButton.x = p * (this.sprLight.width - this.sprButton.width);
			this.sprLight.x = this.sprButton.x - this.sprLight.width + this.sprButton.width/2;
			
			this.txtPercent.text = "" + num;
		}
		
		/**  设置下限 **/
		public function set min(num:Number):void
		{
			this.m_minNumber = Math.floor(num);
		}
		
		/** 设置上限 **/
		public function set max(num:Number):void
		{
			this.m_maxNumber = Math.floor(num);
		}
		
		/*****************************************************************/
		
		private function InitObject():void
		{
			m_minNumber = 0;
			m_maxNumber = 100;
			
			sprLight.x = -sprLight.width;
			txtPercent.text = "0";
		}
		
		private function InitListener():void
		{
			this.sprButton.addEventListener(MouseEvent.MOUSE_DOWN, onBtnStartDrag);
			this.sprButton.addEventListener(MouseEvent.MOUSE_UP, onBtnStopDrag);
		}
		
		/** 开始拖动滑块 **/
		private function onBtnStartDrag(e:MouseEvent):void
		{
			this.sprButton.startDrag(false, new Rectangle(0, this.sprButton.y, this.sprLight.width - this.sprButton.width, 0));
			
			onChangeStatue(e);
			
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onChangeStatue);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, onBtnStopDrag);
		}
		
		/** 停止拖动滑块 **/
		private function onBtnStopDrag(e:MouseEvent):void
		{
			this.sprButton.stopDrag();
			
			onChangeStatue(e);
			
			this.dispatchEvent(new Event(SliderObject.CHANGE_DONE));
			
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onChangeStatue);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, onBtnStopDrag);
		}
		
		/** 修改状态数据值 **/
		private function onChangeStatue(e:MouseEvent):void
		{
			this.sprLight.x = this.sprButton.x - this.sprLight.width + this.sprButton.width/2;
			
			this.txtPercent.text = "" + this.percent;
			
			e.updateAfterEvent();
			
			this.dispatchEvent(new Event(SliderObject.CHANGE_PLACE));
		}
	}
}